package com.m2bshop.shop.controller;

import com.alibaba.fastjson.JSONArray;
import com.m2bshop.shop.pojo.Brand;
import com.m2bshop.shop.pojo.Company;
import com.m2bshop.shop.pojo.Result.Result;
import com.m2bshop.shop.service.BrandService;
import com.m2bshop.shop.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/mvo")
public class CompanyController {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private BrandService brandService;

    /**
     * 新增公司信息
     * @param map
     * @return
     */
    @RequestMapping("/addcom")
    @ResponseBody
    public Result addCom(@RequestBody Map map, HttpSession session){
        System.out.println("here");
        String mvoId = session.getAttribute("username").toString();
        Company company = new Company();
        company.setMvoId(mvoId);
        company.setNameCn(map.get("nameCn").toString());
        company.setNameEn(map.get("nameEn").toString());
        if(map.get("info")==(null))
            company.setInfo("/");
        else
            company.setInfo(map.get("info").toString());
        company.setRType(map.get("rType").toString());
        company.setRUrl(map.get("rUrl").toString());
        String timestr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        company.setCreateUser(mvoId);
        company.setCreateTime(timestr);
        company.setUpdateUser(mvoId);
        company.setUpdateTime(timestr);
        System.out.println(company.getRType()+company.getRUrl());
        System.out.println("eee");
        companyService.addCom(company);
        return Result.ok(company);
    }

    /**
     * 根据品牌商id查询公司信息
     * @param model
     * @return mvoinfo.html
     */
    @RequestMapping("/getmvoinfo")
    public String findComById(Model model, HttpSession session){
        String mvoId = session.getAttribute("username").toString();
        Company com = companyService.findComByMvoId(mvoId);
        if(com==null)
            return "addcompany";
        else {
            System.out.println(com);
            model.addAttribute("companyId", com.getCompanyId());
            model.addAttribute("nameCn", com.getNameCn());
            model.addAttribute("nameEn", com.getNameEn());
            model.addAttribute("info", com.getInfo());
            model.addAttribute("rType", com.getRType());
            model.addAttribute("rUrl", com.getRUrl());

            List<Brand> brands = brandService.findBrandByComId(com.getCompanyId());
            if (brands.size()==0) {
                model.addAttribute("result", "fail");
            } else {
                model.addAttribute("result", "success");
//                model.addAttribute("brands", brands);
            }
            return "mvoinfo";
        }
    }

    /**
     * 根据公司id更新公司信息
     * @param map
     * @return
     */
    @RequestMapping("/updatecom")
    @ResponseBody
    public Result UpdateCom(@RequestBody Map map, HttpSession session){
        System.out.println(map);
        String mvoId = session.getAttribute("username").toString();
        Company company = new Company();
        company.setCompanyId(Integer.parseInt(map.get("companyId").toString()));
        company.setNameCn(map.get("nameCn").toString());
        company.setNameEn(map.get("nameEn").toString());
        if(map.get("info")==(null))
            company.setInfo("/");
        else
            company.setInfo(map.get("info").toString());
        company.setRType(map.get("rType").toString());
        company.setRUrl(map.get("rUrl").toString());
        String timestr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        company.setUpdateUser(mvoId);
        company.setUpdateTime(timestr);
        companyService.updateCom(company);
        System.out.println(company);
        return Result.ok();
    }

    /**
     * 根据页面名称，控制器跳转页面
     * @param name 页面名称
     * @param model
     * @return 页面.html
     */
    @RequestMapping("/go{name}")
    public String jumpHtml(@PathVariable String name, Model model){
        System.out.println(name);
        return name;
    }

//    @RequestMapping("/brand")
//    @ResponseBody
//    public Result brandJSON(Integer companyId){
//        List<Brand> brands = brandService.findBrandByComId(companyId);
//        JSONArray jsonArray = (JSONArray) JSONArray.toJSON(brands);
//        System.out.println(jsonArray);
//        return Result.ok(jsonArray);
//    }

    @RequestMapping("/addbrand")
    public Result addbrand(@RequestBody Brand brand){
        System.out.println("add");
        return Result.ok();
    }

}
